package com.bunnyhsu.service.impl;

import com.bunnyhsu.dto.SuggestsWithPage;
import com.bunnyhsu.mapper.SuggestMapper;
import com.bunnyhsu.model.Image;
import com.bunnyhsu.model.Suggest;
import com.bunnyhsu.service.CommitterService;
import com.bunnyhsu.service.ImageService;
import com.bunnyhsu.service.SuggestService;
import com.bunnyhsu.service.TypeService;
import com.bunnyhsu.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xl on 2018/4/10.
 */
@Service
public class SuggestServiceImpl implements SuggestService{

    @Autowired
    private SuggestMapper suggestMapper;
    @Autowired
    private CommitterService committerService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private ImageService imageService;

    private final int UNAUDITED_STATE = 0;
    private final int AUDITED_STATE = 1;
    private final int REPLIED_STATE = 2;
    private final int SCENE_REPLIED_STATE = -1;


    @Override
    public SuggestsWithPage getUnauditedSuggests(int page, int number)
    {
        //页数计算
        page = (page - 1) * number;
        //获取待收（状态0）的意见
        List<Suggest> suggests = suggestMapper.selectSuggest(page, number, UNAUDITED_STATE);

        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggests(UNAUDITED_STATE,number));
        return suggestsWithPage;
    }

    @Override
    public SuggestsWithPage getAuditedSuggests(int page, int number) {
        //页数计算
        page = (page - 1) * number;
        //获取待办（状态1）的意见
        List<Suggest> suggests = suggestMapper.selectSuggest(page, number, AUDITED_STATE);

        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggests(AUDITED_STATE,number));
        return suggestsWithPage;
    }

    @Override
    public SuggestsWithPage getRepliedSuggests(int page, int number) {
        //页数计算
        page = (page - 1) * number;
        //获取已回复和现场回复（状态-1和2）的意见
        List<Suggest> suggests = suggestMapper
                .selectUnionSuggest(page, number, REPLIED_STATE,SCENE_REPLIED_STATE);
        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggests(REPLIED_STATE, SCENE_REPLIED_STATE,number));
        return suggestsWithPage;
    }

    @Override
    public Integer getPageOfSuggests(int state, int number) {
        Integer sumCount = suggestMapper.selectCountOfSuggests(state);
        double doublePage = (double)sumCount/(double) number;
        Integer page = (int) Math.ceil(doublePage);
        return page;
    }

    @Override
    public Integer getPageOfSuggests(int state1, int state2, int number) {
        Integer sumCount1 = suggestMapper.selectCountOfSuggests(state1);
        Integer sumCount2 = suggestMapper.selectCountOfSuggests(state2);
        double doublePage = ((double)sumCount1+(double)sumCount2)/(double) number;
        Integer page = (int)Math.ceil(doublePage);
        return page;
    }

    @Override
    public boolean updateSuggest2Audited(String reply, int type_id, int id) {

        if(suggestMapper.updateSuggest(AUDITED_STATE, type_id, reply, id)==1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean updateSuggest2Replied(String reply, int type_id, int id) {

        if(suggestMapper.updateSuggest(REPLIED_STATE, type_id, reply, id)==1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public boolean updateSuggest2SceneReplied(int type_id, int id) {

        if(suggestMapper.updateSuggest(SCENE_REPLIED_STATE, type_id, "", id)==1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public SuggestsWithPage getUnauditedSuggestsByDate(int page, int number, String up, String down) {
        page = (page - 1) * number;
        //获取待收（状态0）的意见 根据时间
        List<Suggest> suggests =
                suggestMapper.selectSuggestByDate(page, number, UNAUDITED_STATE ,up ,down);
        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggestsByDate(UNAUDITED_STATE,number,up,down));
        return suggestsWithPage;
    }

    @Override
    public SuggestsWithPage getAuditedSuggestsByDate(int page, int number, String up, String down) {
        page = (page - 1) * number;
        //获取待收（状态1）的意见 根据时间
        List<Suggest> suggests =
                suggestMapper.selectSuggestByDate(page, number, AUDITED_STATE ,up ,down);
        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggestsByDate(AUDITED_STATE,number,up,down));
        return suggestsWithPage;
    }

    @Override
    public SuggestsWithPage getRepliedSuggestsByDate(int page, int number, String up, String down) {
        page = (page - 1) * number;
        //获取已回复（状态-1和2）的意见 根据时间
        List<Suggest> suggests = suggestMapper
                .selectUnionSuggestByDate(page,number,REPLIED_STATE,SCENE_REPLIED_STATE,up,down);
        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfSuggestsByDate(REPLIED_STATE,SCENE_REPLIED_STATE,number,up,down));
        return suggestsWithPage;
    }

    @Override
    public Integer getPageOfSuggestsByDate(int state,int number, String up, String down) {
        Integer sumCount = suggestMapper.selectCountOfSuggestsByDate(state,up,down);
        double doublePage = (double)sumCount/(double) number;
        Integer page = (int) Math.ceil(doublePage);
        return page;
    }

    @Override
    public Integer getPageOfSuggestsByDate(int state1, int state2,int number, String up, String down) {
        Integer sumCount1 = suggestMapper.selectCountOfSuggestsByDate(state1,up,down);
        Integer sumCount2 = suggestMapper.selectCountOfSuggestsByDate(state2,up,down);
        double doublePage = ((double)sumCount1+(double)sumCount2)/(double) number;
        Integer page = (int)Math.ceil(doublePage);
        return page;
    }

    @Override
    public SuggestsWithPage getUnionSuggestByDateAndType
            ( int page, int number, String up, String down, int typeId) {
        page = (page - 1) * number;
        List<Suggest> suggests = suggestMapper
                .selectUnionSuggestByDateAndType(page,number, up, REPLIED_STATE, SCENE_REPLIED_STATE,down,typeId);
        SuggestsWithPage suggestsWithPage = new SuggestsWithPage(translate(suggests),
                getPageOfUnionSuggestsByDateAndType(REPLIED_STATE,SCENE_REPLIED_STATE,number,up,down,typeId));
        return suggestsWithPage;
    }

    @Override
    public Integer getPageOfUnionSuggestsByDateAndType
            (int state1, int state2, int number, String up, String down, int typeId) {
        Integer sumCount = suggestMapper.selectCountOfUnionSuggestsByDateAndType(state1,state2,up,down,typeId);
        double doublePage = (double)sumCount/(double) number;
        Integer page = (int) Math.ceil(doublePage);
        return page;
    }

    @Override
    public Integer addSuggest(String content, String userId) {

        Suggest suggest = new Suggest();
        suggest.setDate(DateUtil.now());
        suggest.setContent(content);
        suggest.setUser_id(userId);
        suggestMapper.insertSuggest(suggest);
        return suggest.getId();
    }

    @Override
    public List<Suggest> getRepliedSuggestByCommitterId(String userId) {
        return suggestMapper.getRepliedSuggestByCommitterId(userId);
    }

    /**
     * @return model转entity
     */
    private List<com.bunnyhsu.dto.Suggest> translate (List<Suggest> suggests)
    {
        List<com.bunnyhsu.dto.Suggest> ldSuggests = new ArrayList<>();
        for (Suggest suggest : suggests)
        {
            ldSuggests.add(new com.bunnyhsu.dto.Suggest(suggest.getId(),suggest.getState(),suggest.getDate(),
                    suggest.getContent(),typeService.getTypeById(suggest.getType_id()),
                    committerService.getCommitter(suggest.getUser_id()), suggest.getReply(),
                    imageService.getImages(suggest.getId())));
        }
        return ldSuggests;
    }

}
